﻿Public Class frmContact_Form 
    Public is_edit As Boolean = False
    Public id As String = ""
    Public old_serverid As String = ""

    Private Sub SimpleButtonSave_Click(sender As Object, e As EventArgs) Handles SimpleButtonSave.Click
        If is_edit Then
            If old_serverid.Equals("") = False And id.Equals("") = False And TextEditName.Text.Equals("") = False And TextEditServerID.Text.Equals("") = False Then
                If mysql_isconnected() Then
                    Dim data As DataTable
                    data = mysql_query("SELECT * FROM tb_contact WHERE email='" & TextEditServerID.Text & "'")
                    If data.Rows.Count <= 0 Or old_serverid.Equals(TextEditServerID.Text) Then
                        mysql_non_query(String.Format("UPDATE tb_contact SET nama_kontak='{0}',no_hp='{1}',email='{2}' WHERE id_kontak='{3}'", TextEditName.Text, TextEditPhone.Text, TextEditServerID.Text, id))
                        Me.Close()
                        frmContact.loadContact()
                    Else
                        MessageBox.Show("Server Contact Already in List")
                    End If

                Else
                    MessageBox.Show("Can't Connect to Database Server")
                End If

            Else
                MessageBox.Show("Please fill all form correctly")
            End If
        Else
            If TextEditName.Text.Equals("") = False And TextEditServerID.Text.Equals("") = False Then
                If mysql_isconnected() Then
                    Dim data As DataTable
                    data = mysql_query("SELECT * FROM tb_contact WHERE email='" & TextEditServerID.Text & "'")
                    If data.Rows.Count <= 0 Then
                        mysql_non_query(String.Format("INSERT INTO tb_contact(nama_kontak,no_hp,email) VALUES ('{0}','{1}','{2}')", TextEditName.Text, TextEditPhone.Text, TextEditServerID.Text))
                        Me.Close()
                        frmContact.loadContact()
                    Else
                        MessageBox.Show("Server Contact already exists")
                    End If
                Else
                    MessageBox.Show("Can't Connect to Database Server")
                End If
            Else
                MessageBox.Show("Please fill all form correctly")
            End If
        End If
    End Sub

    Private Sub frmContact_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If is_edit Then
            Me.Text = "Edit Contact"
            If mysql_isconnected() Then
                Dim data As DataTable
                data = mysql_query("SELECT * FROM tb_contact WHERE id_kontak='" & id & "'")
                If data.Rows.Count > 0 Then
                    TextEditName.Text = data.Rows(0)("nama_kontak")
                    TextEditPhone.Text = data.Rows(0)("no_hp")
                    TextEditServerID.Text = data.Rows(0)("email")
                Else
                    MessageBox.Show("Data Not Found. Please Try Again Later")
                    Me.Close()
                End If
            Else
                MessageBox.Show("Cannot Edit Contact Right Now. Please Try Again Later")
                Me.Close()
            End If
        Else
            Me.Text = "Add Contact"
        End If
    End Sub

    Private Sub SimpleButtonExit_Click(sender As Object, e As EventArgs) Handles SimpleButtonExit.Click
        Me.Close()
    End Sub
End Class